/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.servlets;

import com.oea.hibernate.entities.Exam;
import com.oea.hibernate.entities.Test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author unnati
 */
public class EditTestAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            
            HttpSession s=request.getSession();
           Long id=Long.parseLong(request.getParameter("id"));
            List<Test> list_test=(List<Test>) request.getSession().getAttribute("li_alltests");
           
            for (Test t : list_test)
            {   
                if(id==t.getId())
                {
                     String tdate=t.getTdate();
                     String ttime=t.getTtime();
                     String tduration=t.getDuration();
                     
                    String[] testtime=ttime.split(":");
                    String[] testduration=tduration.split(":");
                    String[] testdate=tdate.split("-");
                    
                    s.setAttribute("tdate_day", testdate[0]);
                    s.setAttribute("tdate_month", testdate[1]);
                    s.setAttribute("tdate_year", testdate[2]);
                    
                     s.setAttribute("ttime_hour", testtime[0]);
                    s.setAttribute("ttime_minute", testtime[1]);
                    s.setAttribute("ttime_second", testtime[2]);
                    
                     s.setAttribute("tduration_hour", testduration[0]);
                    s.setAttribute("tduration_minute", testduration[1]);
                    s.setAttribute("tduration_second", testduration[2]);
                    
                    s.setAttribute("detail", t);
                    break;
                }
                
            }
             RequestDispatcher rd=request.getRequestDispatcher("edit-test.jsp");
            rd.forward(request, response);
            
            //response.sendRedirect("edit-test.jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
